package com.omi.iberolactalpreventa;

import java.util.ArrayList;

import com.omi.iberolactalpreventa.objects.AdapterComentario;
import com.omi.iberolactalpreventa.objects.AdapterConsumos;
import com.omi.iberolactalpreventa.objects.AdapterPedido;
import com.omi.iberolactalpreventa.objects.ItmComentario;
import com.omi.iberolactalpreventa.objects.ItmConsumo;
import com.omi.iberolactalpreventa.objects.ItmPedido;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;

public class DatosCli extends Activity{
	
	private TabHost tabs;
	private EditText  txtCodCliente;
	private EditText txtnombre;
	private EditText txtNifCif;
	private EditText txtDireccion;
	private EditText txtCp;
	private EditText txtPob;
	private EditText txtProv;
	private EditText txtPais;
	private EditText txtTelefono;
	private EditText txtTarifa;
	private EditText txtdtoFactura;
	private EditText txtcomment;
	private ListView lstComments;
	private ListView lstConsumos;
	private ListView lstPedidos;
	private String idClient; 
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_datoscli);
		
		Bundle extras = getIntent().getExtras();
		if(extras!=null)
		{
			idClient = extras.getString("idClient");
		}
				
		tabs = (TabHost) findViewById(android.R.id.tabhost);
		tabs.setup();
		
		TabHost.TabSpec spec = tabs.newTabSpec("General");
		spec.setContent(R.id.General);
		spec.setIndicator("General");
		tabs.addTab(spec);
		
		spec = tabs.newTabSpec("Comentarios");
		spec.setContent(R.id.Comentarios);
		spec.setIndicator("Comentarios");
		tabs.addTab(spec);
		
		spec = tabs.newTabSpec("Pedidos");
		spec.setContent(R.id.Pedidos);
		spec.setIndicator("Pedidos");
		tabs.addTab(spec);
		
		spec = tabs.newTabSpec("Consumos");
		spec.setContent(R.id.Consumos);
		spec.setIndicator("Consumos");
		tabs.addTab(spec);
		
		txtCodCliente = (EditText)findViewById(R.id.txtCodCliente);
		txtnombre= (EditText)findViewById(R.id.txtFechaVisitas);
		txtNifCif= (EditText)findViewById(R.id.txtNifCif);
		txtDireccion= (EditText)findViewById(R.id.txtDireccion);
		txtCp= (EditText)findViewById(R.id.txtCp);
		txtPob= (EditText)findViewById(R.id.txtPob);
		txtProv= (EditText)findViewById(R.id.txtProv);
		txtPais= (EditText)findViewById(R.id.txtPais);
		txtTelefono= (EditText)findViewById(R.id.txtTelefono);
		txtTarifa= (EditText)findViewById(R.id.txtTarifa);
		txtdtoFactura= (EditText)findViewById(R.id.txtdtoFactura);
		lstComments = (ListView)findViewById(R.id.lstComments);
		lstConsumos = (ListView)findViewById(R.id.lstConsumos);
		lstPedidos = (ListView)findViewById(R.id.lstPedidos);
		txtcomment = (EditText)findViewById(R.id.txtcomment);
		
		//Sacamos los datos del cliente para rellenar el formulario:
		Cursor c = Modulebase.db.ejecutarSelect(new String[]{"COD_CLIENTE,NOMBRE,DIRECCION,NIF_CIF,PROVINCIA,POBLACION,TELEFONO,CP,COD_PAIS,TARIFA,DTO_FACTURA"}, "CLIENTE", "COD_CLIENTE=?", new String[]{idClient});

		if(c!=null && c.moveToFirst())
		{
			//Solo vamos a tener una fila asi que no hacemos bucle:
			txtCodCliente.setText(c.getString(0));
			txtnombre.setText(c.getString(1));
			txtDireccion.setText(c.getString(2));
			txtNifCif.setText(c.getString(3));
			txtProv.setText(c.getString(4));
			txtPob.setText(c.getString(5));
			txtTelefono.setText(c.getString(6));
			txtCp.setText(c.getString(7));
			txtPais.setText(c.getString(8));
			txtTarifa.setText(c.getString(9));
			txtdtoFactura.setText(c.getString(10));
		}
		
		//Sacamos los comentarios:
		c = Modulebase.db.ejecutarSelect(new String[]{"COMENTARIO ","FECHA","TIPO","NUM_LIN"},"COMENT_CLIENTE", "COD_CLIENTE=?", new String[]{idClient});
		
		if(c!=null && c.moveToFirst())
		{
		
			ArrayList<ItmComentario> comments = new ArrayList<ItmComentario>();
			do
			{
			ItmComentario comment = new ItmComentario(c.getString(0),c.getString(1), c.getString(2), c.getString(3));
			comments.add(comment);
			}while(c.moveToNext());
			
			AdapterComentario adptr = new AdapterComentario(this, comments);
			
			lstComments.setAdapter(adptr);
		}
		
		//Pedidos:

		c = Modulebase.db.ejecutarSelect(new String[]{"N_Pedido","Fecha_Reg","Su_Ntra_ref"}, "Cab_Pedido", "Venta_a_N_Clie=? AND EXPORTADO=?", new String[]{idClient,"0"});
	
		if(c!=null && c.moveToFirst())
		{
			ArrayList<ItmPedido> peds = new ArrayList<ItmPedido>();
			do
			{
				ItmPedido ped = new ItmPedido(c.getString(0), c.getString(1), c.getString(2));
				peds.add(ped);
			}while(c.moveToNext());
			
			AdapterPedido adptr = new AdapterPedido(this, peds);
			lstPedidos.setAdapter(adptr);
		}
		
		//Consumos:
		c = Modulebase.db.ejecutarRawQuery("SELECT con.COD_PRODUCTO,prod.DESCRIPCION,con.COD_UM,SUM(con.CANTIDAD) as CANTIDAD FROM CONSUMOS con INNER JOIN PRODUCTO prod ON con.COD_PRODUCTO=prod.NUMERO WHERE con.COD_CLIE=? GROUP BY COD_PRODUCTO,COD_CLIE,COD_UM",new String[]{idClient});

		if(c!=null && c.moveToFirst())
		{
			ArrayList<ItmConsumo> consumos = new ArrayList<ItmConsumo>();
			do
			{
				ItmConsumo con = new ItmConsumo(c.getString(0), c.getString(1),c.getString(2),c.getInt(3));
				consumos.add(con);
			}while(c.moveToNext());
			
			AdapterConsumos adptr = new AdapterConsumos(this, consumos);
			lstConsumos.setAdapter(adptr);
		}
		
		
		//Eventos:
		//Al hacer click en un comentario le mostramos:
		lstComments.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				//Sacamos el contenido del mensaje y le mostramos en el textbox:
				ItmComentario comment = (ItmComentario) parent.getItemAtPosition(position);
				txtcomment.setText(comment.getComment());
			}
		});
		
		
		//Al hacer click en pedidos vamos al formulario de pedido rellenando los datos que llevamos:
		lstPedidos.setOnItemClickListener( new OnItemClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
				ItmPedido ped = (ItmPedido)parent.getItemAtPosition(position);
				Intent intVerPed = new Intent("com.omi.iberolactalpreventa.NuevoPedido");
				intVerPed.putExtra("Verped", true);
				intVerPed.putExtra("numPed", ped.getNumPedido());
				startActivity(intVerPed);
			}
		});
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	 @Override
     public boolean onOptionsItemSelected(MenuItem item) {
             switch (item.getItemId()) {
             case android.R.id.home:
              finish();
             default: 
              return super.onOptionsItemSelected(item);      
             }
     }
	
	 @Override
	 public void onBackPressed() {
	 	finish();
	 }
}
